package com.lqwu.mallchatserver.common.event.listener;

import com.lqwu.mallchatserver.chat.service.ChatService;
import com.lqwu.mallchatserver.chat.service.cache.GroupMemberCache;
import com.lqwu.mallchatserver.common.event.GroupMemberAddEvent;
import com.lqwu.mallchatserver.user.model.dto.ChatMessageReq;
import com.lqwu.mallchatserver.user.model.entry.GroupMember;
import com.lqwu.mallchatserver.user.model.entry.RoomGroup;
import com.lqwu.mallchatserver.user.model.entry.User;
import com.lqwu.mallchatserver.user.model.enums.MessageTypeEnum;
import com.lqwu.mallchatserver.user.service.GroupMemberService;
import com.lqwu.mallchatserver.user.service.UserService;
import com.lqwu.mallchatserver.user.service.cache.UserInfoCache;
import com.lqwu.mallchatserver.websocket.model.vo.response.WSBaseResp;
import com.lqwu.mallchatserver.websocket.model.vo.response.data.WSMemberChange;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 群成员添加事件监听器
 */
@Component
public class GroupMemberAddListener {
    @Resource
    private UserInfoCache userInfoCache;
    @Resource
    private ChatService chatService;
    @Resource
    private GroupMemberCache groupMemberCache;
    @Resource
    private UserService userService;

    /**
     * 发送群成员添加消息
     * @param event
     */
    @Async
    @TransactionalEventListener(classes = GroupMemberAddEvent.class,phase = TransactionPhase.AFTER_COMMIT)
    public void sendAddMsg(GroupMemberAddEvent event) {
        // 1.获取参数
        List<GroupMember> memberList = event.getGroupMemberList();
        RoomGroup roomGroup = event.getRoomGroup();
        Long inviteUid = event.getUid();
        // 2.从缓存获取用户信息
        User user = userInfoCache.get(inviteUid);
        // 3.获取添加用户id列表
        List<Long> uidList = memberList.stream().map(GroupMember::getUid).collect(Collectors.toList());
        // 4.构建消息
        ChatMessageReq chatMessageReq = new ChatMessageReq();
        chatMessageReq.setRoomId(roomGroup.getRoomId());
        chatMessageReq.setMsgType(MessageTypeEnum.SYSTEM.getType());
        String msg = memberList.stream()
                .map(member -> userInfoCache.get(member.getUid()).getName())
                .collect(Collectors.joining(","));
        String content = user.getName() + "邀请" + msg + "加入群聊";
        chatMessageReq.setBody(content);
        chatService.sendMsg(chatMessageReq, User.UID_SYSTEM);
    }

    /**
     * 群成员添加成功后需更新缓存
     * @param event
     */
    @Async
    @TransactionalEventListener(classes = GroupMemberAddEvent.class, fallbackExecution = true)
    public void sendChangePush(GroupMemberAddEvent event){
        // 1.获取参数
        List<GroupMember> memberList = event.getGroupMemberList();
        RoomGroup roomGroup = event.getRoomGroup();
        List<Long> memberUidList = groupMemberCache.getMemberUidList(roomGroup.getRoomId());
        List<Long> uidList = memberList.stream().map(GroupMember::getUid).collect(Collectors.toList());
        List<User> users = userService.listByIds(uidList);
        users.forEach(user -> {
            // todo 推送
//            WSBaseResp<WSMemberChange> ws = MemberAdapter.buildMemberAddWS(roomGroup.getRoomId(), user);
//            pushService.sendPushMsg(ws, memberUidList);
        });
        //移除缓存
        groupMemberCache.evictMemberUidList(roomGroup.getRoomId());
    }
}
